<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-28
 * Time: 17:38
 */

namespace app\index\controller;


use app\base\BaseController;
use app\base\model\Borrow as BorrowModel;
use think\Db;
use think\Request;

class Borrow extends BaseController
{
    /***
     * 归还操作
     * @return array
     * @throws \think\exception\DbException
     */
    public function dell()
    {

        $request = Request::instance();
        $borrow_id = $request->has('borrow_id')?$request->param('borrow_id'):false;
        if (!$borrow_id) {
            return ['code' => -1, 'msg' => '参数错误'];
        }

        $book_id=BorrowModel::get($borrow_id)->getData('book_id');



        Db::startTrans();
        try{
            Db::name('borrow')->where('borrow_id', $borrow_id)->delete();
            Db::name('books')->where('book_id', $book_id)->setInc('book_num');
            // 提交事务
            Db::commit();
            return ['code'=>1,'msg'=>'归还成功'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }

    }

}